package com.dhnsoft.数据结构与算法.二叉树;

/**
 * ClassName: BinaryTreeDemo
 * Package: com.dhnsoft.数据结构与算法.二叉树
 * Description:
 *
 * @Author dhn
 * @Create 2024/4/1 21:11
 * @Version 1.0
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode h1 = new HeroNode(1,"宋江");
        HeroNode h2 = new HeroNode(2,"吴用");
        HeroNode h3 = new HeroNode(3,"卢俊义");
        HeroNode h4 = new HeroNode(4,"林冲");
        HeroNode h5 = new HeroNode(5,"关胜");
        binaryTree.setRoot(h1);
        h1.setLeft(h2);
        h1.setRight(h3);
        h3.setRight(h4);
        h3.setLeft(h5);

        System.out.println("前序遍历:");
        binaryTree.preOrder();
        System.out.println("中序遍历:");
        binaryTree.infixOrder();
        System.out.println("后序遍历:");
        binaryTree.postOrder();
    }
}

class BinaryTree{
    private HeroNode root;

    public HeroNode getRoot() {
        return root;
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }
        else{
            System.out.println("二叉树为空，无法遍历");
        }

    }

    public void infixOrder(){
        if(this.root!=null){
            this.root.infixOrder();
        }
        else{
            System.out.println("二叉树为空，无法遍历");
        }
    }

    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }
        else{
            System.out.println("二叉树为空，无法遍历");
        }
    }
}

class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if (this.right!=null){
            this.right.preOrder();
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    public void postOrder(){
        if(left!=null){
            this.left.postOrder();
        }
        if(right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }


    /**
     * 前序查找
     * */
    public HeroNode preOrderSearch(int no){
        //如果当前节点等于查找节点则返回
        if(this.no==no){
            return this;
        }
        //使用一个临时节点来获取数据
        //如果当前节点没找到就开始找左节点
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.preOrderSearch(no);
            return temp;
        }

        if(this.right!=null){
            temp = this.right.preOrderSearch(no);
            return temp;
        }
        return temp;
    }


    /**
     * 中序查找
     * */
    public HeroNode infixOrderSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.infixOrderSearch(no);
            return temp;
        }
        if(this.no==no){
            return this;
        }
        if(this.right!=null){
            temp = this.right.infixOrderSearch(no);
            return temp;
        }
        return temp;
    }

    /**
     * 后续查找
     * */
    public HeroNode postOrderSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = postOrderSearch(no);
            return temp;
        }
        if(this.right!=null){
            temp = postOrderSearch(no);
            return temp;
        }
        if(this.no==no){
            return this;
        }
        return temp;
    }

    
}
